/****************************************************************************
 * Copyright (C) 2011-2013 Dj_Skual
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ***************************************************************************/
#include "WiiSaveBrowser.h"
#include "../../Themes/CTheme.h"
#include "../../Controls/Application.h"

WiiSaveBrowser::WiiSaveBrowser(WiiSave_List * list, GuiFrame * parentElement)
	: GuiFrame(400, 190, parentElement)
	, selectedSave(0)
	, size(list->GetCount())
	, saveList(list)
{
	SetEffect(EFFECT_FADE, 20);
	SetAlignment(ALIGN_CENTER | ALIGN_BOTTOM);
	SetPosition(-6, -30);
	
	btnSoundClick = Resources::GetSound("button_click.wav");
	btnSoundOver = Resources::GetSound("button_over.wav");
	btnSoundClick->SetVolume(Settings.SFXVolume);
	btnSoundOver->SetVolume(Settings.SFXVolume);
	
	imgDialogBox = Resources::GetImageData("browser.png");
	imgLine = Resources::GetImageData("browser_separator.png");
	imgButtonBox = Resources::GetImageData("manage_save_box_button.png");
	
	trigA = new SimpleGuiTrigger(-1, WiiControls.ClickButton | ClassicControls.ClickButton << 16, GCControls.ClickButton);
	trigB = new GuiTrigger();
	trigB->SetButtonOnlyTrigger(-1, WiiControls.BackButton | ClassicControls.BackButton << 16, GCControls.BackButton);
	trigUp = new GuiTrigger();
	trigUp->SetButtonOnlyTrigger(-1, WiiControls.UpButton | ClassicControls.UpButton << 16, GCControls.UpButton);
	trigDown = new GuiTrigger();
	trigDown->SetButtonOnlyTrigger(-1, WiiControls.DownButton | ClassicControls.DownButton << 16, GCControls.DownButton);
	trigLeft = new GuiTrigger();
	trigLeft->SetButtonOnlyTrigger(-1, WiiControls.LeftButton | ClassicControls.LeftButton << 16, GCControls.LeftButton);
	trigRight = new GuiTrigger();
	trigRight->SetButtonOnlyTrigger(-1, WiiControls.RightButton | ClassicControls.RightButton << 16, GCControls.RightButton);
	trigPlus = new GuiTrigger();
	trigPlus->SetButtonOnlyTrigger(-1, WiiControls.NextButton | ClassicControls.NextButton << 16, 0);
	trigMinus = new GuiTrigger();
	trigMinus->SetButtonOnlyTrigger(-1, WiiControls.PrevButton | ClassicControls.PrevButton << 16, 0);
	
	dialogBoxImg = new GuiImage(imgDialogBox);
	dialogBoxImg->SetScaleX(0.616);
	dialogBoxImg->SetScaleY(0.688);
	dialogBoxImg->SetPosition(-110, -40);
	Append(dialogBoxImg);
	
	nameTxt = new GuiText("", 22, thColor("r=0 g=0 b=0 a=255 - download save window text color"));
	nameTxt->SetAlignment(ALIGN_CENTER | ALIGN_TOP);
	nameTxt->SetPosition(-20, 10);
	nameTxt->SetMaxWidth(330, SCROLL_HORIZONTAL);
	Append(nameTxt);
	
	lineImg = new GuiImage(imgLine);
	lineImg->SetScaleY(0.783);
	lineImg->SetAngle(-90);
	lineImg->SetAlignment(ALIGN_CENTER | ALIGN_TOP);
	lineImg->SetPosition(-20, -100);
	Append(lineImg);
	
	synopsisTxt = new GuiLongText((char*)NULL, 18, thColor("r=0 g=0 b=0 a=255 - download save window text color"));
	synopsisTxt->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	synopsisTxt->SetPosition(10, 46);
	synopsisTxt->SetLinesToDraw(6);
	synopsisTxt->SetMaxWidth(340);
	synopsisTxt->SetBreakLine(true);
	Append(synopsisTxt);
	
	numberTxt = new GuiText("", 18, thColor("r=255 g=255 b=255 a=255 - manage windows text color"));
	numberTxt->SetAlignment(ALIGN_LEFT | ALIGN_BOTTOM);
	numberTxt->SetPosition(370, -4);
	Append(numberTxt);
	
	closeBoxImg = new GuiImage(imgButtonBox);
	closeBoxImg->SetAlignment(ALIGN_RIGHT | ALIGN_TOP);
	btnClose = new PictureButton("button_close.png", "button_close_over.png", btnSoundClick, btnSoundOver);
	btnClose->SetAlignment(ALIGN_RIGHT | ALIGN_TOP);
	btnClose->SetPosition(-3, 3);
	btnClose->SetTrigger(trigA);
	btnClose->SetTrigger(trigB);
	btnClose->Clicked.connect(this, &WiiSaveBrowser::OnButtonClick);
	Append(closeBoxImg);
	Append(btnClose);
	
	downloadBoxImg = new GuiImage(imgButtonBox);
	downloadBoxImg->SetAlignment(ALIGN_RIGHT | ALIGN_TOP);
	downloadBoxImg->SetPosition(0, 40);
	btnDownload = new PictureButton("button_valid.png", "button_valid_over.png", btnSoundClick, btnSoundOver);
	btnDownload->SetAlignment(ALIGN_RIGHT | ALIGN_TOP);
	btnDownload->SetPosition(-3, 43);
	btnDownload->SetTrigger(trigA);
	btnDownload->Clicked.connect(this, &WiiSaveBrowser::OnButtonClick);
	Append(downloadBoxImg);
	Append(btnDownload);
	
	previousBoxImg = new GuiImage(imgButtonBox);
	previousBoxImg->SetAlignment(ALIGN_RIGHT | ALIGN_TOP);
	previousBoxImg->SetPosition(0, 80);
	btnPrevious = new PictureButton("button_minus.png", "button_minus_over.png", btnSoundClick, btnSoundOver);
	btnPrevious->SetAlignment(ALIGN_RIGHT | ALIGN_TOP);
	btnPrevious->SetPosition(-3, 83);
	btnPrevious->SetTrigger(trigA);
	btnPrevious->SetTrigger(trigLeft);
	btnPrevious->SetTrigger(trigMinus);
	btnPrevious->Clicked.connect(this, &WiiSaveBrowser::OnChangeSaveClick);
	
	nextBoxImg = new GuiImage(imgButtonBox);
	nextBoxImg->SetAlignment(ALIGN_RIGHT | ALIGN_TOP);
	nextBoxImg->SetPosition(0, 120);
	btnNext = new PictureButton("button_plus.png", "button_plus_over.png", btnSoundClick, btnSoundOver);
	btnNext->SetAlignment(ALIGN_RIGHT | ALIGN_TOP);
	btnNext->SetPosition(-3, 123);
	btnNext->SetTrigger(trigA);
	btnNext->SetTrigger(trigRight);
	btnNext->SetTrigger(trigPlus);
	btnNext->Clicked.connect(this, &WiiSaveBrowser::OnChangeSaveClick);
	
	if(size > 1)
	{
		Append(previousBoxImg);
		Append(nextBoxImg);
		Append(btnPrevious);
		Append(btnNext);
	}
	
	btnUp = new GuiButton(0, 0);
	btnUp->SetTrigger(trigUp);
	btnUp->Clicked.connect(this, &WiiSaveBrowser::OnTextMoveClick);
	Append(btnUp);
	btnDown = new GuiButton(0, 0);
	btnDown->SetTrigger(trigDown);
	btnDown->Clicked.connect(this, &WiiSaveBrowser::OnTextMoveClick);
	Append(btnDown);
	
	ChangeSave(false);
}

WiiSaveBrowser::~WiiSaveBrowser()
{
	parent->Remove(this);
	
	RemoveAll();
	
	Resources::Remove(btnSoundClick);
	Resources::Remove(btnSoundOver);
	
	delete trigA;
	delete trigB;
	delete trigLeft;
	delete trigRight;
	delete trigUp;
	delete trigDown;
	delete trigPlus;
	delete trigMinus;

	Resources::Remove(imgDialogBox);
	Resources::Remove(imgLine);
	Resources::Remove(imgButtonBox);
	
	delete dialogBoxImg;
	delete lineImg;
	delete closeBoxImg;
	delete downloadBoxImg;
	delete nextBoxImg;
	delete previousBoxImg;
	
	delete nameTxt;
	delete numberTxt;
	delete synopsisTxt;
	
	delete btnClose;
	delete btnDownload;
	delete btnNext;
	delete btnPrevious;
	delete btnUp;
	delete btnDown;
}

void WiiSaveBrowser::ChangeSave(bool effect)
{
	static bool fading = false;
	if(fading)
		return;
	fading = true;
	
	if(effect)
	{
		nameTxt->SetEffect(EFFECT_FADE, -20);
		numberTxt->SetEffect(EFFECT_FADE, -20);
		synopsisTxt->SetEffect(EFFECT_FADE, -20);
		
		while(nameTxt->IsAnimated())
			Application::Instance()->updateEvents();
	}
	
	nameTxt->SetText(saveList->GetTitle(selectedSave));
	char number[10];
	snprintf(number, sizeof(number), "%d/%d", selectedSave+1, saveList->GetCount());
	numberTxt->SetText(number);
	synopsisTxt->SetText(saveList->GetSynopsis(selectedSave).c_str());
	synopsisTxt->SetTextLine(0);
	
	if(effect)
	{
		nameTxt->SetEffect(EFFECT_FADE, 20);
		numberTxt->SetEffect(EFFECT_FADE, 20);
		synopsisTxt->SetEffect(EFFECT_FADE, 20);
	}
	
	fading = false;
}

void WiiSaveBrowser::OnButtonClick(GuiButton *sender, int pointer UNUSED, const POINT &p UNUSED)
{
	SetEffect(EFFECT_FADE, -20);
	
	if(sender == btnClose)
		Closed(-1);
	else
		Closed(selectedSave);
}

void WiiSaveBrowser::OnChangeSaveClick(GuiButton *sender, int pointer UNUSED, const POINT &p UNUSED)
{
	if(sender == btnNext)
		selectedSave++;
	else if(sender == btnPrevious)
		selectedSave--;
	
	if(selectedSave >= size)
		selectedSave = 0;
	else if(selectedSave < 0)
		selectedSave = (size - 1);
	
	ChangeSave();
}

void WiiSaveBrowser::OnTextMoveClick(GuiButton *sender, int pointer UNUSED, const POINT &p UNUSED)
{
	if(sender == btnUp)
		synopsisTxt->PreviousLine();
	else
		synopsisTxt->NextLine();
}
